package com.meihua.baoleyi.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.adapters.MyPagerAdapter;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.view.TouchImageView;

import java.util.ArrayList;
import java.util.List;

public class PhotoShowActivity extends BaseActivity {

    private ViewPager vp;//vp
    private LinearLayout ll_xiaoyuand;//标题

    private MyPagerAdapter adapter;
    private List<View> views = new ArrayList<>();
    private List<ImageView> viewsround = new ArrayList<>();
    private TouchImageView img;
    private String[] imageUrlList;
    private int imageIndex;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_photo_show);
        imageIndex = getIntent().getIntExtra("imageIndex", 0);
        String imageUrl = getIntent().getStringExtra("imageUrl");
        imageUrlList = imageUrl.split(",");
        initView();
        initData();
    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        ll_xiaoyuand = (LinearLayout) findViewById(R.id.ll_xiaoyuand);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (imageUrlList.length > 1) {
            for (int i = 0; i < imageUrlList.length; i++) {
                View view = LayoutInflater.from(this).inflate(R.layout.item_round, null);
                ImageView iv_head_round = (ImageView) view.findViewById(R.id.iv_head_round);
                ll_xiaoyuand.addView(view);
                viewsround.add(iv_head_round);
            }
            viewsround.get(imageIndex).setImageResource(android.R.color.white);
        }
    }

    private void initData() {

        if (imageUrlList != null) {
            for (String imageUrl : imageUrlList) {
                View v = LayoutInflater.from(this).inflate(R.layout.item_photo_show, null);
                img = v.findViewById(R.id.img);
                Glide.with(this).load(imageUrl).placeholder(R.mipmap.chanpin).error(R.mipmap.chanpin).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(img);
                views.add(v);
            }
        }
        adapter = new MyPagerAdapter(views);
        vp.setAdapter(adapter);
        vp.setCurrentItem(imageIndex);
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < viewsround.size(); i++) {
                    viewsround.get(i).setImageResource(android.R.color.darker_gray);
                }
                viewsround.get(position).setImageResource(android.R.color.white);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
